package com.almond.redis.handle;

import cn.hutool.extra.spring.SpringUtil;
import com.almond.redis.enums.RedisDelayQueueEnum;
import com.almond.redis.utils.RedisDelayQueueUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 启动延迟队列
 * Created by LPB on 2021/04/20.
 */
@Slf4j
@Component
@Order(100)
public class RedisDelayQueueRunner implements CommandLineRunner {

    @Autowired
    private RedisDelayQueueUtil redisDelayQueueUtil;
    ExecutorService executor = Executors.newFixedThreadPool(10);


    @Override
    public void run(String... args) {
        RedisDelayQueueEnum[] queueEnums = RedisDelayQueueEnum.values();
        for (RedisDelayQueueEnum queueEnum : queueEnums) {
            new Thread(() -> {
                try {
                    while (true) {
                        log.info("zzzzz");
                        Object value = redisDelayQueueUtil.getDelayQueue(queueEnum.getCode());
                        RedisDelayQueueHandle redisDelayQueueHandle = SpringUtil.getBean(queueEnum.getBeanId());
                        redisDelayQueueHandle.execute(value);
                    }
                } catch (InterruptedException e) {
                    log.error("(Redis延迟队列异常中断) {}", e.getMessage());
                }
            }).start();
        }
        log.info("(Redis延迟队列启动成功)");
    }


//    @Override
//    public void run(String... args) throws Exception {
//        RedisDelayQueueEnum[] queueEnums = RedisDelayQueueEnum.values();
//        for (RedisDelayQueueEnum queueEnum : queueEnums) {
//            executor.submit(() -> {
//                    try {
//                            while (true) {
//                                log.info("hhhh");
//                                Object value = redisDelayQueueUtil.getDelayQueue(queueEnum.getCode());
//                                RedisDelayQueueHandle redisDelayQueueHandle = SpringUtil.getBean(queueEnum.getBeanId());
//                                redisDelayQueueHandle.execute(value);
//                            }
//                    } catch (InterruptedException e) {
//                        log.info("err");
//                        e.printStackTrace();
//                    }
//            });
//        }
//
//    }


//    @Override
//    public void run(String... args) throws Exception {
//        RedisDelayQueueEnum[] queueEnums = RedisDelayQueueEnum.values();
//        for (RedisDelayQueueEnum queueEnum : queueEnums) {
//            executor.submit(() -> {
//                try {
//                    while (true) {
//                        log.info("hhhh");
//                        Object value = redisDelayQueueUtil.getDelayQueue(queueEnum.getCode());
//                        RedisDelayQueueHandle redisDelayQueueHandle = SpringUtil.getBean(queueEnum.getBeanId());
//                        redisDelayQueueHandle.execute(value);
//                    }
//                } catch (InterruptedException e) {
//                    log.info("err");
//                    e.printStackTrace();
//                }
//            });
//        }
//
//    }

}